package org.lmj.dos.web.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.lmj.dos.common.bean.ServerResponse;
import org.lmj.dos.common.pojo.*;
import org.lmj.dos.mapper.BaseAttrInfoMapper;
import org.lmj.dos.mapper.BaseAttrValueMapper;
import org.lmj.dos.mapper.ProductAttrValueMapper;
import org.lmj.dos.mapper.ProductMapper;
import org.lmj.dos.web.resultdo.ProductDetailVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RequestMapping
@RestController
public class ProductController {
    @Autowired
    ProductMapper productMapper;
    @Autowired
    BaseAttrInfoMapper baseAttrInfoMapper;
    @Autowired
    BaseAttrValueMapper baseAttrValueMapper;
    @Autowired
    ProductAttrValueMapper productAttrValueMapper;

    @GetMapping("/products")
    public ServerResponse productsList( String categoryId) throws IOException {
        QueryWrapper<ProductEntity> wrapper = new QueryWrapper<>();
        wrapper.in("category_id",categoryId);
        List<ProductEntity> productEntityList1 = productMapper.selectList(wrapper);
        Map<String, Object> map = new HashMap<>();
        map.put("productList",productEntityList1);
        return ServerResponse.success(map);
    }

    /**
     * 查询最新商品
     */
    @GetMapping("/loadLatestProductList")
    public ServerResponse loadLatestProductList()  {
        List<ProductEntity> productEntityList = productMapper.latestProductList();
        return ServerResponse.success(productEntityList);
    }
    /**
     */
    @GetMapping("/productListByType/{categoryId}/{type}")
    public ServerResponse productListByType(@PathVariable("type") Integer type,@PathVariable("categoryId") Integer categoryId)  {
        HashMap<String, Integer> stringIntegerHashMap = new HashMap<>();
        stringIntegerHashMap.put("type",type);
        stringIntegerHashMap.put("categoryId",categoryId);
        List<ProductEntity> productEntityList = productMapper.productListByType(stringIntegerHashMap);
        return ServerResponse.success(productEntityList);
    }
    @GetMapping("/search/{keyword}/{type}")
    public ServerResponse productListByType(@PathVariable("type") Integer type,@PathVariable("keyword") String keyword)  {
        HashMap<String, Object> stringIntegerHashMap = new HashMap<>();
        stringIntegerHashMap.put("type",type);
        stringIntegerHashMap.put("keyword",keyword);
        List<ProductEntity> productEntityList = productMapper.searchByKeyword(stringIntegerHashMap);
        return ServerResponse.success(productEntityList);
    }

    @GetMapping("/products/{productId}")
    public ServerResponse productsDetail(@PathVariable("productId") Integer productId) throws IOException {
        QueryWrapper<ProductEntity> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(ProductEntity::getId,productId);
        ProductEntity productEntity = productMapper.selectOne(wrapper);
        ProductDetailVo productDetailVo = new ProductDetailVo();
        productDetailVo.setAttrInners(new ArrayList<>());
        BeanUtils.copyProperties(productEntity,productDetailVo);
        QueryWrapper<ProductAttrValueEntity> wrapper1 = new QueryWrapper<>();
        wrapper1.lambda().in(ProductAttrValueEntity::getProductId,productId);
        List<ProductAttrValueEntity> productAttrValueEntities = productAttrValueMapper.selectList(wrapper1);
        for (ProductAttrValueEntity productAttrValueEntity:productAttrValueEntities){
            QueryWrapper<BaseAttrInfoEntity> baseAttrInfoEntityQueryWrapper = new QueryWrapper<>();
          baseAttrInfoEntityQueryWrapper.lambda().eq(BaseAttrInfoEntity::getId, productAttrValueEntity.getAttrId());
            BaseAttrInfoEntity baseAttrInfoEntity = baseAttrInfoMapper.selectOne(baseAttrInfoEntityQueryWrapper);
            String name="";
            if(baseAttrInfoEntity!=null){
                name=baseAttrInfoEntity.getName();
            }
            QueryWrapper<BaseAttrValueEntity> wrapper2 = new QueryWrapper<>();
            wrapper2.lambda().in(BaseAttrValueEntity::getAttrId,productAttrValueEntity.getAttrId());
            List<String> values = baseAttrValueMapper.selectList(wrapper2).stream().map(BaseAttrValueEntity::getAttrValue).collect(Collectors.toList());
            ProductDetailVo.AttrInner build = ProductDetailVo.AttrInner.builder().name(name).values(values).build();
            productDetailVo.getAttrInners().add(build);
        }
        return ServerResponse.success(productDetailVo);
        //https://www.jianshu.com/p/7d7e5e4e8ae3
//        ClassPathResource classPathResource = new ClassPathResource("product-45-productDetail.json");
//        InputStream inputStream =classPathResource.getInputStream();
//        ObjectMapper objectMapper = new ObjectMapper();
//
//        ProductVo.GetProductDetail getProducts = objectMapper.readValue(inputStream, ProductVo.GetProductDetail.class);
//        ServerResponse<ProductVo.GetProductDetail> success = ServerResponse.success(getProducts);
//        return success;
    }
}
